package com.chinasoft.news.config;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInterceptor;
import org.apache.ibatis.plugin.Interceptor;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;
import java.util.Properties;


public class MybatisConfig {


    @Bean
    public PageInterceptor pageHelper(){
        PageInterceptor pi = new PageInterceptor();
        Properties properties = new Properties();
//        分页工具的方言（定义数据库的类型）
        properties.setProperty("helperDialect","mysql");
        pi.setProperties(properties);
        return pi;
    }

    @Bean
    public SqlSessionFactoryBean sqlSessionFactory(DataSource ds,Interceptor pi) {
        SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
        factory.setDataSource(ds);
        factory.setPlugins(new Interceptor[]{pi});
        return  factory;
    }

    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer() {
        MapperScannerConfigurer mapperScanner = new MapperScannerConfigurer();
        mapperScanner.setBasePackage("com.chinasoft.news.mapper");
        return  mapperScanner;
    }
}
